package com.example.hotel.web;

import com.example.hotel.common.JsonResult;
import com.example.hotel.entity.Room;
import com.example.hotel.service.RoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "获取房间信息")
@RequestMapping("room")
@RestController
@CrossOrigin
public class RoomController {
    @Autowired
    RoomService roomService;
    @Autowired
    private Room room;

    @ApiOperation(value = "查询房间")
    @GetMapping("findAll")
    public JsonResult findAllRoom() {
        try {
            List<Room> rooms= roomService.findAllRoom();
            if (!rooms.isEmpty()) {
                return JsonResult.ok(rooms);
            } else {
                return JsonResult.fail(5001, "房间查询失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();}
    }



    @ApiOperation(value = "添加房间")
    @GetMapping("add")
    public JsonResult addRoom(Room room) {
        try {
            int r = roomService.insert(room);
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5001, "房间添加失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();
        }
    }

    @ApiOperation(value = "删除房间")
    @DeleteMapping("delete")
    public JsonResult deleteRoom( Integer roomId) {
        try {
            if (roomId == null) {
                return JsonResult.fail(5002, "房间id不能为空");
            }
            int r = roomService.deleteById(roomId);
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5002, "房间删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();
        }
    }

    @ApiOperation(value = "更新房间")
    @GetMapping("update")
    public JsonResult updateRoom(@ApiParam(name = "roomId", value = "1", required = true)Integer roomId,@ApiParam(name = "roomType", value = "1", required = true)String roomType,@ApiParam(name = "roomState", value = "1", required = true)String roomState,@ApiParam(name = "price", value = "1", required = true)float price) {
        try {
            int r = roomService.update(roomId,roomType,roomState,price);
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5001, "房间更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();
        }

    }




}
